<template>
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->

        <el-select v-model="query.operationCode" clearable filterable
                   placeholder="工序代码" style="width: 185px;" class="filter-item" @change="crud.toQuery" >
          <el-option
            v-for="item in operations"
            :key="item.id"
            :label="item.operationName"
            :value="item.operationCode" >
            <span style="float: left">{{ item.operationCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.operationName }}</span>
          </el-option>
        </el-select>


        <el-select v-model="query.materialCode" clearable filterable
                   placeholder="物料编码" style="width: 185px;" class="filter-item" @change="crud.toQuery" >
          <el-option
            v-for="item in materials"
            :key="item.id"
            :label="item.materialName"
            :value="item.materialCode" >
            <span style="float: left">{{ item.materialCode }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.materialName }}</span>
          </el-option>
        </el-select>

        <el-select v-model="query.codingType" clearable filterable
                   placeholder="编码类型" style="width: 105px;" class="filter-item" @change="crud.toQuery" >
          <el-option
            v-for="item in dict.coding_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

        <el-select v-model="query.isNecessary" clearable filterable
                   placeholder="是否必传" style="width: 105px;" class="filter-item" @change="crud.toQuery" >
          <el-option
                  v-for="item in dict.whether"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form :inline="true"  ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="物料编码" prop="materialCode">
            <el-select
              v-model="form.materialCode"
              filterable
              placeholder="物料编码"
              style="width: 530px"
            >
              <el-option
                v-for="item in materials"
                :key="item.id"
                :label="item.materialName"
                :value="item.materialCode"
              >
                <span style="float: left">{{ item.materialCode }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{
                    item.materialName
                  }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工序代码" prop="operationCode">
            <el-select
              v-model="form.operationCode"
              filterable
              placeholder="工序代码"
              style="width: 530px"
            >
              <el-option
                v-for="item in operations"
                :key="item.id"
                :label="item.operationName"
                :value="item.operationCode"
              >
                <span style="float: left">{{ item.operationCode }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{
                    item.operationName
                  }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用量" prop="quantity">
            <el-input-number v-model="form.quantity" :step="1" step-strictly style="width: 220px;"></el-input-number>
          </el-form-item>
          <el-form-item label="物料单位" prop="materialUnit">
            <el-input v-model="form.materialUnit" step-strictly style="width: 220px;"></el-input>
          </el-form-item>
          <el-form-item label="损耗率" prop="attritionRate">
            <el-input-number v-model="form.attritionRate" :step="0.1" step-strictly style="width: 220px;"></el-input-number>
          </el-form-item>
          <el-form-item label="编码类型 " prop="codingType">
            <el-radio-group v-model="form.codingType" style="width: 220px">
              <el-radio  v-for="item in dict.coding_type" :label="item.value">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否必传" prop="isNecessary">
            <el-radio-group v-model="form.isNecessary" style="width: 220px">
              <el-radio  v-for="item in dict.whether" :label="item.value">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 530px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="operationCode" label="工序编号" width="100px;" align="center"/>
        <el-table-column label="工序名称" width="150px;" align="center">
          <template slot-scope="scope">
            {{ getOperationName(scope.row.operationCode) }}
          </template>
        </el-table-column>
        <el-table-column prop="materialCode" label="物料编码" width="150px;" align="center" />
        <el-table-column  label="物料名称" width="150px;" align="center" >
          <template slot-scope="scope">
            {{ getMaterialName(scope.row.materialCode) }}
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="用量" width="100px;" align="center"/>
        <el-table-column prop="materialUnit" label="物料单位" width="100px;" align="center"/>
        <el-table-column prop="attritionRate" label="损耗率（%）" width="100px;" align="center"/>
        <el-table-column prop="codingType" label="编码类型 " width="100px;" align="center">
          <template slot-scope="scope">
            {{ dict.label.coding_type[scope.row.codingType] }}
          </template>
        </el-table-column>
        <el-table-column prop="isNecessary" label="是否必传" width="100px;" align="center">
          <template slot-scope="scope">
            {{ dict.label.whether[scope.row.isNecessary] }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="描述" width="250px;" align="center"/>
        <el-table-column prop="createBy" label="创建人" width="100px;" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="150px;" align="center"/>
        <el-table-column prop="updateBy" label="更新人" width="100px;" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="150px;" align="center"/>
        <el-table-column v-if="checkPer(['admin','productBomDetail:edit','productBomDetail:del'])" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
</template>

<script>
import crudProductBomDetail from '@/api/product/productBomDetail'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import craftOperationProduct from "@/api/craft/craftOperationProduct";
import productMaterial from "@/api/product/productMaterial";

const defaultForm = { id: null, bomId: null, materialCode: null, operationCode: null, quantity: null, materialUnit: null, attritionRate: null, codingType: '0', isNecessary: '1', remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'ProductBomDetail',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['coding_type', 'whether'],
  props: {
    initBomTree: {
      type: Function
    }
  },
  cruds() {
    return CRUD({
      title: 'BOM详情',
      url: 'api/productBomDetail',
      idField: 'id',
      sort: 'id,desc',
      queryOnPresenterCreated: false,
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: false,
        reset: true,
        import: false,
      },
      crudMethod: { ...crudProductBomDetail }})
  },
  data() {
    return {
      productCode: "",
      bomId: 0,
      operations: [],
      materials: [],
      permission: {
        add: ['admin', 'productBomDetail:add'],
        edit: ['admin', 'productBomDetail:edit'],
        del: ['admin', 'productBomDetail:del']
      },
      rules: {
        materialCode: [
          { required: true, message: '物料编码不能为空', trigger: 'blur' }
        ],
        operationCode: [
          { required: true, message: '工序编号不能为空', trigger: 'blur' }
        ],
        quantity: [
          { required: true, message: '用量不能为空', trigger: 'blur' }
        ],
        materialUnit: [
          { required: true, message: '物料单位不能为空', trigger: 'blur' }
        ],
        attritionRate: [
          { required: true, message: '损耗率不能为空', trigger: 'blur' }
        ],
        codingType: [
          { required: true, message: '编码类型 不能为空', trigger: 'blur' }
        ],
        isNecessary: [
          { required: true, message: '是否必传不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'bomId', display_name: '清单ID' },
        { key: 'materialCode', display_name: '物料编码' },
        { key: 'operationCode', display_name: '工序编号' },
        { key: 'quantity', display_name: '用量' },
        { key: 'materialUnit', display_name: '物料单位' },
        { key: 'attritionRate', display_name: '损耗率' },
        { key: 'codingType', display_name: '编码类型 ' },
        { key: 'isNecessary', display_name: '是否必传' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.bomId = this.bomId;
      if(this.initBomTree instanceof Function){
        this.initBomTree(this.bomId);
      }
      return true
    },
    //新增之前
    [CRUD.HOOK.beforeToAdd]() {
      this.crud.form.bomId = this.bomId;
      return true
    },
    //查询页面
    queryPage(id,productCode) {
      // 获取工序代码
      craftOperationProduct.getByProduct({productCode: productCode}).then((data) => {
        this.operations = data;
      });

      this.bomId = id;

      this.crud.toQuery();

    },
    //获取物料名称
    getMaterialName(materialCode){
      let name = '';
      this.materials.forEach(item =>{
        if(item.materialCode == materialCode){
          name = item.materialName;
          return ;
        }
      })
      return name;
    },
    //获取工序名称
    getOperationName(operationCode){
      let name = '';
      this.operations.forEach(item =>{
        if(item.operationCode == operationCode){
          name = item.operationName;
          return ;
        }
      })
      return name;
    }
  },
  mounted() {

    // 获取物料信息
    productMaterial.query({size: 1000}).then((data) => {
      this.materials = data.content;
    });
  }
}
</script>

<style scoped>

</style>
